/**
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.profile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.active.DictionaryAdapter;
import com.tstudy.laoshibang.active.DictionaryAdapter.ActionCallBack;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.event.SelectAddressEvent;
import com.tstudy.laoshibang.login.NeedLoginFragment;
import com.tstudy.laoshibang.manager.CityManager;
import com.tstudy.laoshibang.mode.City;
import com.tstudy.laoshibang.mode.Dictionary;
import com.tstudy.laoshibang.utils.LogUtil;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import de.greenrobot.event.EventBus;
@EFragment(R.layout.select_address)
public class SelectAddressFragment extends NeedLoginFragment{
	final static String TAG = "select_address";
	public SelectAddressFragment() {
		super();
	}
	
	@ViewById(R.id.select_address_root)
	RelativeLayout mRootLayout;
	
	@ViewById(R.id.select_address_province_list)
	ListView  provinceListView;
	
	@ViewById(R.id.select_address_city_list)
	ListView  cityListView;
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;
	List<Dictionary> provinceList;
	List<Dictionary> cityList;
	DictionaryAdapter provinceAdapter;
	DictionaryAdapter cityAdapter;
	Dictionary mSelectProvince;
	Dictionary mSelectCity;
//	Dictionary mSelectCounty;
	

	public static void add(int containerViewId,Dictionary selectProvince,Dictionary selectCity) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,SelectAddressFragment_.class.getName(),args);
		((SelectAddressFragment)fragment).mSelectProvince = selectProvince;
		((SelectAddressFragment)fragment).mSelectCity = selectCity;
		((BaseActivity) BaseApplication.mCurrentActivity).add(containerViewId, fragment, TAG);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		if (mIsFirstLoad) {
			mRootLayout.setOnClickListener(null);
			provinceAdapter = new DictionaryAdapter();
			cityAdapter = new DictionaryAdapter();
			provinceListView.setAdapter(provinceAdapter);
			cityListView.setAdapter(cityAdapter);
			provinceAdapter.setData(provinceList);
			provinceList = buildList(Dictionary.TYPE_PROVINCE,null);
			if (mSelectProvince != null) {
				cityList = buildList(Dictionary.TYPE_CITY,mSelectProvince);
			}else{
				cityList = buildList(Dictionary.TYPE_CITY,provinceList.get(0));
			}
			provinceAdapter.setData(provinceList);
			cityAdapter.setData(cityList);
			provinceAdapter.setActionCallBack(new ActionCallBack() {
				@Override
				public void onItemClick(Dictionary dictionary,int position) {
					if (mSelectProvince != null) {
						mSelectProvince.isSelected =  false;
					}
					mSelectProvince = dictionary;
					mSelectProvince.isSelected = true;
					provinceAdapter.setData(provinceList);
					cityList = buildList(Dictionary.TYPE_CITY,dictionary);
					cityAdapter.setData(cityList);
				}
			});
			cityAdapter.setActionCallBack(new ActionCallBack() {
				@Override
				public void onItemClick(Dictionary dictionary,int position) {
					if (mSelectCity != null) {
						mSelectCity.isSelected =  false;
					}
					mSelectCity = dictionary;
					mSelectCity.isSelected = true;
					SelectAddressEvent addressEvent = new SelectAddressEvent();
					addressEvent.selectType = Dictionary.TYPE_CITY;
					addressEvent.firstCol = mSelectProvince;
					addressEvent.secondCol = dictionary;
					cityAdapter.setData(cityList);
					EventBus.getDefault().post(addressEvent);
					backAction(mFragmentId);
				}
			});
			
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Dictionary> buildList(String type,Dictionary dictionary){
		List<City> sourceList = null;
		if (Dictionary.TYPE_PROVINCE.equals(type)) {
			sourceList = CityManager.getInstance().queryByField("parentProvinceId",0);
		}else if (Dictionary.TYPE_CITY.equals(type)) {
			if (BaseApplication.mLocationCity != null) {
				sourceList = CityManager.getInstance().queryByField("parentProvinceId", BaseApplication.mLocationCity.getCityId());
			}else{
//				cityList = CityManager.getInstance().queryByField("parentProvinceId",dictionary.key);
				Map<String, Object> args = new HashMap<String, Object>();
				args.put("parentProvinceId", dictionary.code+"");
				args.put("parentCityId","0");
				sourceList = CityManager.getInstance().queryByFields(args);
			}
		}
		List<Dictionary> dicList = new ArrayList<Dictionary>();
		if (sourceList != null) {
			int size = sourceList.size();
			for (int i = 0; i < size; i++) {
				Dictionary each = new Dictionary();
				City city = sourceList.get(i);
				each.type = type;
				each.code = city.getCityId();
				each.name = city.getCityName();
				if (Dictionary.TYPE_PROVINCE.equals(type)) {
					if (mSelectProvince != null && each.code == mSelectProvince.code ) {
						mSelectProvince = each;
						each.isSelected = true;
					}else if(mSelectProvince == null && i==0){//默认选择第一个
						mSelectProvince = each;
						each.isSelected = true;
					}
				}else if(Dictionary.TYPE_CITY.equals(type)){
					if (mSelectCity != null && each.code == mSelectCity.code) {
						mSelectCity = each;
						each.isSelected = true;
					}else if(mSelectCity == null && i==0){//默认选中第一个
						mSelectCity = each;
						each.isSelected = true;
					}
				}
				dicList.add(each);
			}
		}
		return dicList;
	}
	
	@Click({R.id.select_address_back,R.id.select_address_commit})
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.select_address_back:
			backAction(mFragmentId);
			break;
		case R.id.select_address_commit:
			break;
		default:
			break;
		}
	}
	
	@Override
	public void clear() {
		
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}

}
